﻿//-----------------------------------------------------------------------
// <copyright file="PivotcollectionProject.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Library
{
    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using PivotCollectionTools.Data;

    /// <summary>
    /// Represents a project with data for building and publishing Pivot Collections.
    /// </summary>
    [Serializable]
    public class PivotCollectionProject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PivotCollectionProject"/> class.
        /// </summary>
        public PivotCollectionProject()
        {
            this.Table = new PivotTable();
        }

        /// <summary>
        /// Gets or sets the collection title.
        /// </summary>
        /// <value>The collection title of the pivot collection project.</value>
        public string CollectionTitle { get; set; }

        /// <summary>
        /// Gets the table.
        /// </summary>
        /// <value>The table of the pivot collection project.</value>
        public PivotTable Table { get; set; }

        /// <summary>
        /// Gets or sets the publish folder.
        /// </summary>
        /// <value>The publish folder of the pivot collection project.</value>
        public string PublishFolder { get; set; }

        /// <summary>
        /// Gets or sets the converted image folder.
        /// </summary>
        /// <value>The converted image folder of the pivot collection project.</value>
        public string ConvertedImageFolder { get; set; }

        /// <summary>
        /// Gets or sets the name of the deep zoom collection.
        /// </summary>
        /// <value>The name of the deep zoom collection.</value>
        public string DeepZoomCollectionName { get; set; }

        /// <summary>
        /// Gets or sets the name of the pivot collection file.
        /// </summary>
        /// <value>The name of the pivot collection file.</value>
        public string PivotCollectionFileName { get; set; }

        /// <summary>
        /// Loads the project.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>A <see cref="PivotCollectionProject"/> object based on the specified filed name.</returns>
        public static PivotCollectionProject LoadProject(string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Open);
            BinaryFormatter formatter = new BinaryFormatter();
            PivotCollectionProject project = (PivotCollectionProject)formatter.Deserialize(stream);
            stream.Close();
            return project;
        }

        /// <summary>
        /// Saves the project.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public void SaveProject(string fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
            stream.Close();
        }

        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            this.Table.Clear();
            this.CollectionTitle = string.Empty;
            this.ConvertedImageFolder = string.Empty;
            this.DeepZoomCollectionName = string.Empty;
            this.PivotCollectionFileName = string.Empty;
            this.PublishFolder = string.Empty;
        }
    }
}
